package com.cdd.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdd.commonutils.JwtUtils;
import com.cdd.commonutils.MD5;
import com.cdd.servicebase.handler.SmartException;
import com.cdd.ucenter.entity.Member;
import com.cdd.ucenter.entity.vo.LoginVo;
import com.cdd.ucenter.entity.vo.RegisterVo;
import com.cdd.ucenter.mapper.MemberMapper;
import com.cdd.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author cdd
 * @since 2021-02-13
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(LoginVo loginVo) {
        String email = loginVo.getEmail();
        String password = loginVo.getPassword();

        // 判断邮箱或密码是否为空
        if(StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            throw new SmartException(20001, "邮箱或用户名为空");
        }

        // 判断邮箱是否存在
        QueryWrapper<Member> wrapperEmail = new QueryWrapper<>();
        wrapperEmail.eq("email",email);
        Member one = baseMapper.selectOne(wrapperEmail);
        if(StringUtils.isEmpty(one)) {
            throw new SmartException(20001, "登录失败");
        }
        // 判断密码是否正确 MD5临时的，没有加盐值
        if(!MD5.encrypt(password).equals(one.getPassword())) {
            throw new SmartException(20001, "登录失败");
        }
        // 判断是否被禁用
        if(one.getIsDisabled()) {
            throw new SmartException(20001, "登录失败");
        }

        // 登陆成功，使用JWT工具生成token
        String jwtToken = JwtUtils.getJwtToken(one.getId(), one.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        // 获取注册信息
        String code = registerVo.getCode();
        String email = registerVo.getEmail();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        // 非空判断
        if(StringUtils.isEmpty(code) || StringUtils.isEmpty(email)
                || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)) {
            throw new SmartException(20001, "注册失败");
        }

        // 判断验证码
        String redisCode = redisTemplate.opsForValue().get(email);
        if(!redisCode.equals(code)) {
            throw new SmartException(20001, "注册失败");
        }

        // 判断邮箱是否相同
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("email", email);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0) {
            throw new SmartException(20001, "注册失败");
        }

        // 添加数据
        Member member = new Member();

        Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        member.setGmtCreate(localDateTime);
        member.setGmtModified(localDateTime);
        member.setNickname(nickname);
        member.setEmail(email);
        member.setPassword(MD5.encrypt(password));
        member.setAvatar("https://cdd-lh.oss-cn-beijing.aliyuncs.com/2021/01/31/c6a2cdaa04a3487d8141c58d275eb87bfile.png"); // 默认头像
        baseMapper.insert(member);
    }

    @Override
    public Integer countRegister(String day) {
        Integer integer = baseMapper.countRegisterDay(day);
        return integer;
    }

}
